package net.confinanz.client.data;

import java.sql.ResultSet;
import java.sql.SQLException;

import net.confinanz.client.controller.Controller;
import net.confinanz.client.exceptions.NotConnectedException;

public class Konto {
	
	static final String SELECT 			= "SELECT * FROM `ent_konto`";
	static final String INSERT 			= "INSERT INTO `confinanz`.`ent_konto` (`konto_inhaber`, `konto_nummer`, `konto_blz`, `konto_bank`) VALUES (";
	static final String UPDATE 			= "UPDATE `ent_konto` SET ";
	
	private String id;
	private String inhaber;
	private String kontonummer;
	private String bankleitzahl;
	private String name;
		
	public Konto() {
		this.id 			= "";
		this.inhaber 		= "";
		this.kontonummer 	= "";
		this.bankleitzahl 	= "";
		this.name 			= "";
	}
	
	public Konto(String id, Controller controller) throws SQLException, NotConnectedException {
		setId(id);
		fromDatabase(controller);
	}
		

//	##### Instanz Methoden #####

	public String getId() {
		return id;
	}
	
	private void setId(String id) {
		this.id = id;
	}
	
	public String getInhaber() {
		return inhaber;
	}

	public void setInhaber(String inhaber) {
		this.inhaber = inhaber;
	}

	public String getKontonummer() {
		return kontonummer;
	}

	public void setKontonummer(String kontonummer) {
		this.kontonummer = kontonummer;
	}

	public String getBankleitzahl() {
		return bankleitzahl;
	}

	public void setBankleitzahl(String bankleitzahl) {
		this.bankleitzahl = bankleitzahl;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String toStringExtended() {
		return (this.id + " : " + this.inhaber + " : " + this.kontonummer + " : " + this.bankleitzahl + " : " + this.name);
	}
	
	public String toStringDatabase() {
		return toStringExtended();
	}


//	##### Datenbank Methoden #####
	
	public void fromDatabase(Controller controller) throws SQLException, NotConnectedException {
		String sql = SELECT + "WHERE `konto_id` = " + getId();
		controller.logMessage("SQL-QUERY: " + sql);
		ResultSet result = controller.getConnector().select(sql);
		if (result.first()) {
			setKontonummer(result.getString("konto_nummer"));
			setBankleitzahl(result.getString("konto_blz"));
			setName(result.getString("konto_bank"));
			setInhaber(result.getString("konto_inhaber"));
			
			controller.logMessage("RESULT: " + toStringDatabase());
		} else throw new SQLException("There is no dataset with `konto_id` = " + getId() + " in table `ent_konto`.");
	}

	public void toDatabase(Controller controller) throws SQLException, NotConnectedException {
		String sql;
		if (getId().equals("")) {
			sql = INSERT + "'" + getInhaber() 
					+ "', '" + getKontonummer()
					+ "', '" + getBankleitzahl()
					+ "', '" + getName()
					+ "')";
			
			controller.logMessage("SQL-QUERY: " + sql);
			controller.getConnector().insert(sql);
						
			sql = Controller.INSERT_ID;
			controller.logMessage("SQL-QUERY: " + sql);
			ResultSet result = controller.getConnector().select(sql);
			
			if (result.first()) {
				setId(result.getString("id"));
			}
			controller.logMessage("RESULT: " + toStringDatabase());
		} else {
			sql = UPDATE + "`konto_nummer` = '" + getKontonummer() + "'"
					+ ", `konto_blz` = '" + getBankleitzahl() + "'"
					+ ", `konto_bank` = '" + getName() + "'"
					+ ", `konto_inhaber` = '" + getInhaber() + "'"
					+ " WHERE `konto_id` = " + getId();
			
			controller.logMessage("SQL-QUERY: " + sql);
			controller.getConnector().insert(sql);
			controller.logMessage("RESULT: " + toStringDatabase());
		}
	}
	
	
//	##### Override Methoden #####
	
	@Override
	public String toString() {
		return getName() + " Kto.: " + getKontonummer() + " BLZ:" + getBankleitzahl();
	}
	
	@Override
	public boolean equals(Object object) {
		return ((object instanceof Konto) && (((Konto) object)).getId().equals(this.getId()));
	}
}